package com.sec.android.app.myfiles.facade.cmd;

import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.CategoryType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.navigation.NavigationManager;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

public class LaunchPickerCmd extends SimpleCommand {

    private static boolean sIsDownload = false;

    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();
        try {
            int processId = (Integer) params[0];
            FileRecord homePath = (FileRecord) params[1];
            NavigationMode mode = (NavigationMode) params[2];
            String[] mimeType = (String[]) params[3];
            String[] extension = (String[]) params[4];
            int maxCount = (Integer) params[5];
            String storagePath = (String) params[6];
            sIsDownload = (boolean) params[7];
            CategoryType category = (CategoryType) params[8];
            String prefix = (String) params[9];

            NavigationManager navigationManager = NavigationManager.getInstance(processId);
            if (!navigationManager.isFromPrivateBox())
                navigationManager.clear();

            NavigationInfo navigationInfo = NavigationInfo.getInstance(mode, homePath);
            navigationInfo.setCategoryFilter(category);
            navigationInfo.setMimeTypeFilter(mimeType);
            navigationInfo.setExtensionFilter(extension);
            navigationInfo.setPathFilter(storagePath);
            navigationInfo.setMaxSelectCnt(maxCount);
            navigationInfo.setPrefixFilter(prefix);
            surveyForPicker(navigationInfo);

            navigationManager.enter(navigationInfo);
        } catch (ArrayIndexOutOfBoundsException e) {
            Log.e(this, "There are times when params's length is 8");
        }
    }

    public static boolean isDownload() {
        return sIsDownload;
    }

    private void surveyForPicker(NavigationInfo navigationInfo) {
        SamsungAnalyticsLog.ScreenPath screenPath = navigationInfo.isSinglePickerMode() ? SamsungAnalyticsLog.ScreenPath.FROM_OTHER_SINGLE : SamsungAnalyticsLog.ScreenPath.FROM_OTHER;
        navigationInfo.setScreenPath(screenPath);
    }
}
